package com.hano.swing;

import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.fonts.roboto.FlatRobotoFont;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;
import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.hano.swing.ui.layout.MainFrame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import raven.toast.Notifications;

import javax.swing.*;

import java.awt.*;

import static javax.swing.GroupLayout.Alignment.LEADING;

/**
 * @author bws
 * @version 1.0.0
 * @description: 启动类
 * @date 2025/10/30
 */
@SpringBootApplication
public class Starter extends JFrame {

    private static Starter starter;

    private final MainFrame mainFrame;

    /**
     * 构造函数
     */
    public Starter(){
        initComponents();
        setSize(new Dimension(1366, 768));
        // 将程序放在屏幕中间  https://www.cnblogs.com/explore-03/p/17591034.html
        setLocationRelativeTo(null);

        // 初始化界面
        mainFrame = new MainFrame();
        setContentPane(mainFrame);
        getRootPane().putClientProperty(FlatClientProperties.FULL_WINDOW_CONTENT, true);
        Notifications.getInstance().setJFrame(this);
    }

    public static void main(String args[]) {
        FlatRobotoFont.install();
        FlatLaf.registerCustomDefaultsSource("com.hano.swing.ui.theme");
        UIManager.put("defaultFont", new Font(FlatRobotoFont.FAMILY, Font.PLAIN, 13));
        // 默认主题 - mac light模式
        FlatMacDarkLaf.setup();
        java.awt.EventQueue.invokeLater(() -> {
            starter = new Starter();
            starter.setVisible(true);
        });
    }

    public static void showForm(Component component) {
        component.applyComponentOrientation(starter.getComponentOrientation());
        starter.mainFrame.showForm(component);
    }

    /**
     * 初始化组件
     */
    public void initComponents(){
        /**
         * https://www.apiref.com/java11-zh/java.desktop/javax/swing/WindowConstants.html
         * DISPOSE_ON_CLOSE dispose-window默认窗口关闭操作。
         * DO_NOTHING_ON_CLOSE 无操作默认窗口关闭操作。
         * EXIT_ON_CLOSE 退出应用程序默认窗口关闭操作。
         * HIDE_ON_CLOSE 隐藏窗口默认窗口关闭操作
         */
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        getContentPane().setLayout(groupLayout);

        // https://www.apiref.com/java8/javax/swing/GroupLayout.html
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(LEADING).addGap(0, 719, Short.MAX_VALUE)
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 521, Short.MAX_VALUE)
        );
        pack();
    }

    /**
     * 返回当前类的版本号
     * @return 版本号
     */
    public String getVersion(){
        return "1.0.0";
    }

}
